package com.itheima.responsibility.jdk;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zzw
 * @date 2025/10/25
 * @description 模拟实现过滤器链FilterChain
 */
public class FilterChain {

    private final List<Filter> filters = new ArrayList<>();

    private int index = 0;

    // 链式调用
    public FilterChain addFilter(Filter filter) {
        this.filters.add(filter);
        return this;
    }

    public void doFilter(Request request, Response response) {
        if (index == filters.size()) {
            return;
        }
        Filter filter = filters.get(index);
        index++;
        filter.doFilter(request, response, this);
    }
}